package net.lab1024.sa.admin.module.business.erp.sao.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SiordaDao;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SnordDao;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SnordaDao;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SiordaEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SnordEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SnordaEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.form.SnordAddForm;
import net.lab1024.sa.admin.module.business.erp.sao.domain.form.SnordQueryForm;
import net.lab1024.sa.admin.module.business.erp.sao.domain.form.SnordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.sao.domain.form.SnordaAddForm;
import net.lab1024.sa.admin.module.business.erp.sao.domain.vo.SnordVO;
import net.lab1024.sa.admin.module.business.erp.sao.domain.vo.SnordaVO;
import net.lab1024.sa.base.common.code.ErpErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SnordService {

    @Resource
    private SnordDao snordDao;
    @Resource
    private SnordaDao snordaDao;
    @Resource
    private SiordaDao siordaDao;

    /**
     * 分页查询
     */
    public PageResult<SnordVO> queryPage(SnordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<SnordVO> list = snordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }


    @Transactional
    public ResponseDTO<Long> add(SnordAddForm addForm) {
        SnordEntity snordEntity = SmartBeanUtil.copy(addForm, SnordEntity.class);
        snordDao.insert(snordEntity);
        for (SnordaAddForm snordaAddForm : addForm.getLista()) {
            SnordaEntity copy = SmartBeanUtil.copy(snordaAddForm, SnordaEntity.class);
            copy.setId(snordEntity.getId());
            snordaDao.insert(copy);
        }
        return ResponseDTO.ok(snordEntity.getId());
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(SnordUpdateForm updateForm) {
        SnordEntity snordEntity = SmartBeanUtil.copy(updateForm, SnordEntity.class);
        snordDao.updateById(snordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        snordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }


    @Transactional
    public ResponseDTO<String> delete(Long id) {







        List<SiordaEntity> siordaEntities = siordaDao.selectList(new LambdaQueryWrapper<SiordaEntity>().eq(SiordaEntity::getBaseId, id));
        if (!siordaEntities.isEmpty()){
            return ResponseDTO.error(ErpErrorCode.PARAM_ERROR,"存在销售发货单，禁止删除！");
        }




        snordaDao.deleteById(id);
        snordDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<SnordVO> info(Long id) {
        SnordEntity snordEntity = snordDao.selectById(id);
        SnordVO copy = SmartBeanUtil.copy(snordEntity, SnordVO.class);
        List<SnordaVO> snordaVOS = SmartBeanUtil.copyList(snordaDao.selectList(new LambdaQueryWrapper<SnordaEntity>().eq(SnordaEntity::getId, id)), SnordaVO.class);
        copy.setLista(snordaVOS);
        return ResponseDTO.ok(copy);
    }

    public ResponseDTO<SnordVO> infoUnexecute(Long id) {
        List<SnordVO> poordVOS = snordDao.infoUnexecute(id);
        return ResponseDTO.ok(poordVOS.get(0));

    }
}
